package com.lottery.material.controller.portal.report;

import com.lottery.material.controller.base.BaseController;
import com.lottery.material.enumeration.ResultEnum;
import com.lottery.material.jedis.JedisClient;
import com.lottery.material.pojo.article.ArticleInfo;
import com.lottery.material.pojo.portaluser.CommentInfo;
import com.lottery.material.pojo.portaluser.PortalUserInfo;
import com.lottery.material.pojo.report.ReportRecord;
import com.lottery.material.resolver.CustomException;
import com.lottery.material.service.portal.article.ArticleInfoService;
import com.lottery.material.service.portal.report.ReportRecordService;
import com.lottery.material.service.portal.user.CommentInfoService;
import com.lottery.material.service.portal.user.PortalUserInfoService;
import com.lottery.material.util.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Description:    举报Controller
 * Author:         sunny
 * CreateDate:     2019-04-13 17:49
 */
@RequestMapping("/portal/report")
@Slf4j
@Controller
public class PortalReportController extends BaseController {

    @Autowired
    @Qualifier("jedisClientCluster")
    private JedisClient jedisClient;

    @Autowired
    private ReportRecordService reportRecordService;
    @Autowired
    private PortalUserInfoService portalUserInfoService;
    @Autowired
    private ArticleInfoService articleInfoService;
    @Autowired
    private CommentInfoService commentInfoService;

    /**
     * description: 举报
     * author sunny
     * createDate 2019-04-13 下午 6:20
     * @param request
     * @return
     */
    @RequestMapping(value = "/goReport", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult getReportTypeList(HttpServletRequest request) {
        Map<String, String> reqParamMap = new HashMap<>();
        //解析参数
        if (ParameterUtil.analyticParameter(request, reqParamMap)) {
            throw new CustomException(ResultEnum.PARSE_EXCEPTION);
        }

        //非空判断及用户登录是否过期验证
        ParameterUtil.checkLogin(reqParamMap, jedisClient, portalUserInfoService);

        ResponseResult respDate = validateEmpty(reqParamMap);
        if (null != respDate) {
            return respDate;
        }

        String reportScope = reqParamMap.get("reportScope");    //1：用户；2：文章；3：评论
        if (StringUtils.isBlank(reportScope)){
            return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_NOT_NULL, "reportScope");
        }else{
            if(!"1".equals(reportScope) && !"2".equals(reportScope) && !"3".equals(reportScope)){
                return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_ERROR, "reportScope");
            }else if("1".equals(reportScope) || "2".equals(reportScope)){
                String reportCode = reqParamMap.get("reportCode");
                if (StringUtils.isBlank(reportCode)) {
                    return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_NOT_NULL, "reportCode");
                }else{
                    if("RYF00001".equals(reportCode) || "RWZ00001".equals(reportCode)){
                        String reportContent = reqParamMap.get("reportContent");
                        if(StringUtils.isBlank(reportContent)){
                            return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_NOT_NULL, "reportContent");
                        }
                    }
                }
            }
        }
        String beReportObjectId = reqParamMap.get("beReportObjectId");
        if (StringUtils.isBlank(beReportObjectId)){
            return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_NOT_NULL, "beReportObjectId");
        }

        //签名验证
        ResponseResult responseResult = ParameterUtil.checkSign(reqParamMap);
        if(null != responseResult){
            return responseResult;
        }
        try {
            if ("1".equals(reportScope)){
                PortalUserInfo portalUserInfo = portalUserInfoService.findByUserId(beReportObjectId);
                if(Objects.isNull(portalUserInfo)){
                    return ResponseResult.buildResult(ResultEnum.USER_NOT_EXIST_FAIL);
                }
            }else if("2".equals(reportScope)){
                ArticleInfo articleInfo = articleInfoService.findByArticleId(beReportObjectId);
                if(Objects.isNull(articleInfo)){
                    return ResponseResult.buildResult(ResultEnum.ARTICLE_NOT_EXIST_FAIL);
                }
            }else if("3".equals(reportScope)){
                CommentInfo commentInfo = commentInfoService.findByCommentId(beReportObjectId);
                if(Objects.isNull(commentInfo)){
                    return ResponseResult.buildResult(ResultEnum.COMMENT_NOT_EXIST_FAIL);
                }
            }

            //封装数据进行保存
            ReportRecord reportRecord = new ReportRecord();
            reportRecord.setCreateTime(CalendarUtil.getSystemCurrentDate());
            reportRecord.setReportScope(Integer.valueOf(reportScope));
            reportRecord.setReportCode(reqParamMap.get("reportCode"));
            reportRecord.setReportContent(reqParamMap.get("reportContent"));
            reportRecord.setReportUserId(reqParamMap.get("userId"));
            reportRecord.setBeReportObjectId(beReportObjectId);
            reportRecord.setResultCode(1);  //设置待审核
            Integer count = reportRecordService.saveReportRecord(reportRecord);
            if(-1 == count){
                return ResponseResult.buildResult(ResultEnum.REPORT_FAIL);
            }
            return responseResult.buildResult(ResultEnum.REPORT_SUCCESS);
        }catch (Exception e){
            log.error("【举报出现异常】:{}", ExceptionUtil.logExceptionStack(e));
            throw new CustomException(ResultEnum.REPORT_FAIL);
        }
    }
}
